<?php

function isLogin()
{
	$arr['admin_id'] = session('admin_id');
    if($arr['admin_id'] == '')
    {
        return 1;
    }
}

/*计算会员的相关信息*/
//计算消费积分
function upoints($userid)
{
	$logModel = M('log');
	$userModel = M('user');

	$arr['user_Id'] = $userid;
	$arr['reason'] = '消费积分';
	$res = $logModel->where($arr)->select();

	//定义一个变量，用于保存积分
	$log['pay_points'] = 0;
	for ($i=0; $i < count($res); $i++) { 
		$log['pay_points'] = $res[$i]['points'] + $log['pay_points'];
	}

	//计算有几个积分单元
	$configModer = M('config');
	$configRes   = $configModer->select();
    $weight = $configRes[0]['weight'];
	
	$log['unit'] = floor($log['pay_points']/$weight);
	if($log['unit'] < 0)
	{
		$log['unit'] = 0;
	}

	$Model = new \Think\Model();
	$new_point = $Model->execute("update user set pay_points=".$log['pay_points'].", unit =".$log['unit']." where user_Id = ".$arr['user_Id']);

}
//计算可返积分单元
function num($id)
{
	upoints($id);
	$arr['user_id'] = $id;
	$arr['is_user'] = 1;
	$weightModel = M('weight');
    $weightRes = $weightModel->where($arr)->select();
    $back = 0;
    for ($i=0; $i < count($weightRes); $i++) { 
		$back = $weightRes[$i]['back'] + $back;
	}
	$configModer = M('config');
	$configRes   = $configModer->select();
    $weight = $configRes[0]['weight'];
    $unit = floor($back/$weight);
	if($unit < 0)
	{
		$unit = 0;
	}

	$userModel = M('user');
	$user['user_Id'] = $id;
	$uRes = $userModel->where($user)->select();

	$user['isunit'] = $uRes[0]['unit'] - $unit;
	$Model = new \Think\Model();
	$new_point = $Model->execute("update user set isunit =".$user['isunit']." where user_Id = ".$user['user_Id']);
}


/*计算返还积分*/
function uretPoints($userid)
{
	num($userid);
	$logModel = M('log');
	$userModel = M('user');

	$arr['user_Id'] = $userid;
	$resUser = $userModel->where($arr)->select();
	$arr['reason'] = '赠送积分';
	$res = $logModel->where($arr)->select();

	//定义一个变量，用于保存积分
	$log['return_points'] = 0;
	for ($i=0; $i < count($res); $i++) { 
		$log['return_points'] = $res[$i]['points'] + $log['return_points'];
	}

	/*计算weight里面的返还积分*/
	$wModel = M('weight');
	$w['user_id'] = $userid;
	$w['is_user'] = 1;
	$wRes = $wModel->where($w)->select();
	for ($j=0; $j < count($wRes); $j++) { 
		$log['return_points'] = $wRes[$j]['back'] + $log['return_points'];
	}
	
	/*计算weight里面的返还积分end*/

	$Model = new \Think\Model();
	$new_point = $Model->execute("update user set return_points= '".$log['return_points']."' where user_Id = ".$arr['user_Id']);
}

/*计算可提现积分*/
function uusePoints($userid)
{
	uretPoints($userid);
	$payModel = M('pay');
	$userModel = M('user');

	//查询会员信息
	$arr['user_Id'] = $userid;
	$resUser = $userModel->where($arr)->select();
	/*会员提现通过的记录*/
  	$arr['user_id'] = $userid;
    $arr['is_user'] = 1;
    $arr['apply'] = 1;
    $arr['isApply'] = 1;
    
    $payRes = $payModel->where($arr)->select();

    $res = array();
    for ($i=0; $i < count($payRes); $i++) { 
        //判断此记录是不是提现记录
        $resMoney = strstr($payRes[$i]['money'], '-');
        if($resMoney)
        {
            $res[$i] = $payRes[$i];
        }
    }
	
	$pay['use_points'] = $resUser[0]['return_points'];
	for ($i=0; $i < count($res); $i++) { 
		$pay['use_points'] = $res[$i]['money'] + $pay['use_points'];
	}
	/*会员提现记录end*/

	/*会员提现申请中的的记录*/
  	$arr1['user_id'] = $userid;
    $arr1['is_user'] = 1;
    $arr1['apply'] = 0;
    $arr1['isApply'] = 0;
    
    $payRes1 = $payModel->where($arr1)->select();

    $res1 = array();
    for ($i=0; $i < count($payRes1); $i++) { 
        //判断此记录是不是提现记录
        $resMoney1 = strstr($payRes1[$i]['money'], '-');
        if($resMoney1)
        {
            $res1[$i] = $payRes1[$i];
        }
    }
	
	for ($i=0; $i < count($res1); $i++) { 
		$pay['use_points'] = $res1[$i]['money'] + $pay['use_points'];
	}
	/*会员提现记录end*/

	$Model = new \Think\Model();
	$new_point = $Model->execute("update user set use_points= '".$pay['use_points']."' where user_Id = ".$arr1['user_id']);
}

/*管理员日志*/
function adminLog($title, $arr)
{
	if($title == 'userDel'){
		$user['admin_id'] = $arr['admin_id'];
		$user['admin_name'] = $arr['admin_name'];
		$user['object_id'] = $arr['object_id'];
		$user['phone'] = $arr['phone'];
		$user['title'] = '删除会员';
		$user['is_user'] = '1';
		$user['time'] = date('YmdHis', time());
		$user['year'] = date('Y', time());
		$user['money'] = date('m', time());
		$user['day'] = date('d', time());
		$adminLogModel = M('admin_log');
		$res = $adminLogModel->data($user)->add();
	}elseif($title == 'shopDel'){
		$shop['admin_id'] = $arr['admin_id'];
		$shop['admin_name'] = $arr['admin_name'];
		$shop['object_id'] = $arr['object_id'];
		$shop['phone'] = $arr['phone'];
		$shop['title'] = '删除商家';
		$shop['is_user'] = '0';
		$shop['time'] = date('YmdHis', time());
		$shop['year'] = date('Y', time());
		$shop['money'] = date('m', time());
		$shop['day'] = date('d', time());
		$adminLogModel = M('admin_log');
		$res = $adminLogModel->data($shop)->add();
	}elseif($title == 'givePoints'){
		$points['admin_id'] = $arr['admin_id'];
		$points['admin_name'] = $arr['admin_name'];
		$points['object_id'] = $arr['object_id'];
		$points['phone'] = $arr['phone'];
		$points['title'] = '积分充值';
		$points['is_user'] = '0';
		$points['points'] = $arr['points'];
		$points['time'] = date('YmdHis', time());
		$points['year'] = date('Y', time());
		$points['money'] = date('m', time());
		$points['day'] = date('d', time());
		$adminLogModel = M('admin_log');
		$res = $adminLogModel->data($points)->add();
	}
}



